﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using UnityEditor;
using UnityEngine;

public class NAudioEditor : MonoBehaviour
{
    [MenuItem("Tools/Lame/SetDefineSymbols")]
    static void SetDefineSymbols()
    {
        string[] versionSymbols = new[] { "NAudio" };

        foreach (var targetGroup in GetValidBuildTargetGroups())
        {
            string[] currentSymbols = PlayerSettings.GetScriptingDefineSymbolsForGroup(targetGroup)
                .Split(';')
                .Distinct()
                .OrderBy(symbol => symbol, StringComparer.Ordinal)
                .ToArray();

            string[] newSymbols = currentSymbols.Where(symbol => !symbol.StartsWith("NAudio", StringComparison.Ordinal))
                .Union(versionSymbols)
                .ToArray();

            if (!currentSymbols.SequenceEqual(newSymbols))
            {
                PlayerSettings.SetScriptingDefineSymbolsForGroup(targetGroup, string.Join(";", newSymbols));
            }
        }

    }

#if UNITY_EDITOR
    public static BuildTargetGroup[] GetValidBuildTargetGroups()
    {
        return Enum.GetValues(typeof(BuildTargetGroup)).Cast<BuildTargetGroup>().Where(group =>
        {
            if (group == BuildTargetGroup.Unknown)
            {
                return false;
            }

            string targetGroupName = Enum.GetName(typeof(BuildTargetGroup), group);
            FieldInfo targetGroupFieldInfo = typeof(BuildTargetGroup).GetField(targetGroupName, BindingFlags.Public | BindingFlags.Static);
            bool validReturn = (targetGroupFieldInfo != null && targetGroupFieldInfo.GetCustomAttributes(typeof(ObsoleteAttribute), false).Length == 0);
#if UNITY_WSA
                if (targetGroupName == "Metro" || targetGroupName == "WSA")
                {
                    validReturn = (targetGroupFieldInfo != null);
                }
#endif
            return validReturn;
        }).ToArray();
    }
#endif
}
